package com.cyl.manager.act.service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.service.MyBaseService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.livesass.ProductVideoService;
import com.ruoyi.livesass.api.sync.SysConfigUtils;

import cn.hutool.core.util.StrUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.cyl.manager.act.domain.entity.CouponActivity;
import com.cyl.manager.act.domain.entity.CouponCategory;
import com.cyl.manager.act.mapper.CouponActivityMapper;
import com.cyl.manager.act.mapper.CouponCategoryMapper;
import com.cyl.manager.act.pojo.query.CouponCategoryQuery;
import com.cyl.manager.lbs.domain.LbsProduct;
import com.cyl.manager.lbs.mapper.LbsProductMapper;
import com.cyl.manager.pms.domain.entity.Product;

/**
 * 福利券分类Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class CouponCategoryService extends MyBaseService<CouponCategoryMapper, CouponCategory>{
    @Autowired
    private CouponCategoryMapper couponCategoryMapper;

    @Autowired
    private ProductVideoService productVideoService;
    
    @Autowired
    SysConfigUtils sysConfigUtils;
    
    @Autowired
    private CouponActivityMapper couponActivityMapper;
    
    @Autowired
    private RedisCache redisCache;
    
    /**
     * 查询福利券分类
     *
     * @param id 福利券分类主键
     * @return 福利券分类
     */
    public CouponCategory selectById(Long id) {
        return couponCategoryMapper.selectById(id);
    }
    
    public CouponCategory selectCheckById(Long id) {
    	String key = "COUPON_CATEGORY_CACHE_"+id;
    	CouponCategory couponCategory = redisCache.getCacheObject(key);
    	if(couponCategory==null) {
    		couponCategory = couponCategoryMapper.selectById(id);
    	    redisCache.setCacheObject(key, couponCategory, 5, TimeUnit.SECONDS);

    	}
    	return couponCategory;
    }
 
    

    /**
     * 查询福利券分类列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 福利券分类
     */
    public List<CouponCategory> selectListAll() {
        QueryWrapper<CouponCategory> qw = new QueryWrapper<>();
        return couponCategoryMapper.selectList(qw);
    }
    
    /**
     * 查询福利券分类列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 福利券分类
     */
    public List<CouponCategory> selectList(CouponCategoryQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<CouponCategory> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        Long qrDeptZbjId = query.getDeptZbjId();
        if(qrDeptZbjId!=null) {
            qw.eq("dept_zbj_id", qrDeptZbjId);
        }
        String title = query.getTitle();
        if (!StringUtils.isEmpty(title)) {
            qw.eq("title", title);
        }
        String ms = query.getMs();
        if (!StringUtils.isEmpty(ms)) {
            qw.eq("ms", ms);
        }
        String lx = query.getLx();
        if (!StringUtils.isEmpty(lx)) {
            qw.eq("lx", lx);
        }
        String pic = query.getPic();
        if (!StringUtils.isEmpty(pic)) {
            qw.eq("pic", pic);
        }
        return couponCategoryMapper.selectList(qw);
    }

    /**
     * 新增福利券分类
     *
     * @param couponCategory 福利券分类
     * @return 结果
     */
    public int insert(CouponCategory couponCategory) {
        couponCategory.setDelFlag(0);
        couponCategory.setCreateTime(LocalDateTime.now());
        couponCategory.setDeptZbjId(SecurityUtils.getDeptZbjId());
        
    	String pic = couponCategory.getPic();
    	String des = couponCategory.getMs();
    	String name = couponCategory.getTitle();
    	
    	
    	String subUserName = sysConfigUtils.getSubUserName(couponCategory.getDeptZbjId());
    	
    	if(StrUtil.isBlankIfStr(subUserName)) {
    		throw new RuntimeException("请使用直播间账户创建福利券!");
    	}
    	
    	
    	String ywid = sysConfigUtils.getYwid(couponCategory.getDeptZbjId());
    	
    	Long id = productVideoService.addActivityCoupon(name, pic, des, subUserName , ywid );
    	couponCategory.setId(id);
    	
    	
        return couponCategoryMapper.insert(couponCategory);
    }

    /**
     * 修改福利券分类
     *
     * @param couponCategory 福利券分类
     * @return 结果
     */
    public int update(CouponCategory couponCategory) {
    	String pic = couponCategory.getPic();
    	String des = couponCategory.getMs();
    	String name = couponCategory.getTitle();
    	productVideoService.updateCoupon(couponCategory.getId(), name, pic, des);
        return couponCategoryMapper.updateById(couponCategory);
    }

    /**
     * 批量删除福利券分类
     *
     * @param ids 需要删除的福利券分类主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
    	if(ids.length!=1) {
	        throw new RuntimeException("操作失败，不能批量操作！");
    	}
      	QueryWrapper<CouponActivity> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("categoryid", ids[0]);
    	if(couponActivityMapper.selectCount(queryWrapper)>0) {
            throw new RuntimeException("此分类有优惠券关联不能删除");
    	}
        return couponCategoryMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除福利券分类信息
     *
     * @param id 福利券分类主键
     * @return 结果
     */
    public int deleteById(Long id) {
    	QueryWrapper<CouponActivity> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("categoryid", id);
    	if(couponActivityMapper.selectCount(queryWrapper)>0) {
            throw new RuntimeException("此分类有优惠券关联不能删除");
    	}
        Long[] ids = {id};
    	productVideoService.deleteCoupon(id);
        return couponCategoryMapper.updateDelFlagByIds(ids);
    }
}
